package com.daqian.chance.common.config;

import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.daqian.chance.core.util.MD5Util;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 默认消息转换器配置类
 * fastjson
 *
 * @author fengshuonan
 * @date 2017-05-23 22:56
 */
@Configuration
public class DefaultHttpMessageConvertersConfig {
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        // 将不是String类型的key转换成String类型，否则前台无法将Json字符串转换成Json对象
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat, SerializerFeature.WriteNonStringKeyAsString);

        fastJsonConfig.setFeatures(Feature.AllowArbitraryCommas, Feature.AllowUnQuotedFieldNames, Feature.DisableCircularReferenceDetect);
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteNullStringAsEmpty);
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");

        // 中文乱码解决方案
        List<MediaType> fastMediasTypes = new ArrayList<>();
        // 设定Json格式且编码为utf-8
        fastMediasTypes.add(MediaType.APPLICATION_JSON);
        fastConverter.setSupportedMediaTypes(fastMediasTypes);

        fastConverter.setFastJsonConfig(fastJsonConfig);
        return new HttpMessageConverters(fastConverter);
    }

    public static void main(String[] args) throws Exception {
        String str1 = "channel=enotification&nonce=1669131086YbYAej&secret=9IdoIMk5nqtLireKpw&timestamp=1669131086&comName=testname&comPhone=none&comMail=123@qq.com&customer=测试客户编号001&code=testcno&order=OD1003&track=none&sdop=none&product=液氦&item=none&receive=none&driver=none&dPhone=none&escort=none&headNo=none&backNo=none&area=none";
        String encrypt = MD5Util.encrypt(str1);
        System.out.println(encrypt);
        //\345\244\247\345\256\266\345\245\275\343\200\202
        String code = "\\346\\262\\241\\346\\234\\211\\345\\217\\257\\350\\275\\254\\345\\217\\221\\347\\232\\204\\345\\256\\242\\346\\234\\215";
        //1.获取八进制数据
        String[] split = code.substring(1).split("\\\\");

        StringBuffer s16 = new StringBuffer();

        //2.把八进制转换成格式为:%+十六进制
        for (String s : split) {
            s16.append("%" + Integer.toHexString(Integer.valueOf(s, 8)).toString().toUpperCase());
        }

        //3.解码
        String decode = URLDecoder.decode(s16.toString(), "UTF-8");
        System.out.println("decode = " + decode);

        System.out.println(s16);
    }

}
